const sqlite3 = require('sqlite3').verbose();
function selectPassword(service, account,callback) {
    let cmd=`select * from pwd where service='${service}' and account='${account}'` ;
    console.log(cmd);
    conn.get(cmd,(err, row)=>{
        console.log("err:", err,"row:",row)
        if(err!==null){
            console.log(err);
        }else{
            callback(row)
        }
    });
}
function setPassword(service, account,password) {
    selectPassword(service,account,(r)=>{
        if(r){//find
            updatePassword(service, account,password)
        }else{
            createPassword(service, account,password)
        }
    })
}
function createPassword(service, account,password) {
    let cmd=`insert into pwd(service,account,password) values('${service}','${account}','${password}')` ;
    console.log(cmd)
    const stmt = conn.prepare(cmd);
    stmt.run();
}
function updatePassword(service, account,password) {
    let cmd="update pwd set password=? where service=? and account=?" ;
    const stmt = conn.prepare(cmd);
    stmt.run(password,service,account);
}
function createTable() {
    conn.exec("CREATE TABLE pwd (service TEXT, account TEXT, password TEXT, PRIMARY KEY (service, account))",(e)=>{
        console.log("createTable error",e);
    });
}
function selectAccount(service,callback) {
    let cmd=`select * from pwd where service='${service}'` ;
    console.log(cmd);
    conn.all(cmd,(err, row)=>{
        console.log("err:", err,"row:",row)
        if(err!==null){
            console.log(err);
        }else{
            callback(row)
        }
    });
}
let filename=__dirname+"/key.sqlite"
let conn = new sqlite3.Database(filename);
function main(){
    // createTable()
    selectAccount("localhost",(r)=>{
        console.log(r);
    })
}
// main()
module.expors={selectAccount,createTable,setPassword,selectPassword}
